package com.j2110.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

public class DSUtil {
    private static Connection conn =null;
    private static DataSource ds =null;
    static{
        Properties p = new Properties();
        try {
            p.load(DSUtil.class.getResourceAsStream("/db.properties"));
            ds = BasicDataSourceFactory.createDataSource(p);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取数据库连接对象
     * @return
     */
    public static Connection getConn(){
        try {
            conn = ds.getConnection();
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static<T> List<T> query(String sql,Class<T> clazz,Object ... args){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<T> list = new ArrayList<>();
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i,args[i-1]);//给？赋值。
            }
            rs = ps.executeQuery();  // 执行sql语句
            while (rs.next()){  // 遍历结果
                //创建t对象
                T t = clazz.newInstance();  // 创建对象，目的是保存遍历的每一条记录。
                ResultSetMetaData rsm = rs.getMetaData(); //元数据
                HashMap<String,Object> hm = new HashMap<>();   // 创建集合，目的是，保存每个字段的数据，也就对应每一行记录。
                for (int i = 1; i <= rsm.getColumnCount(); i++) {
                   hm.put(rsm.getColumnName(i),rs.getObject(rsm.getColumnName(i)));
                }
                BeanUtils.populate(t,hm);
                list.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(rs,ps, conn);
        }
        return list;
    }

    /**
     * 增删改
     * @param sql
     * @return
     */
    public static int execute(String sql,Object ... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i,args[i-1]);
            }
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(ps, conn);
        }
        return 0;
    }
    public static void close (PreparedStatement ps, Connection conn){
        close(null,ps,conn);
    }
    public static void close (ResultSet rs ,PreparedStatement ps, Connection conn){
        try {
            if (rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null){
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if (conn != null){
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}